package com.kkd.kkdapp.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KkdPdfWebActivity;
import com.kkd.kkdapp.http.entity.FinancingArgeementEntityItem;
import com.kkd.kkdapp.listener.OnSingleClickListener;

public class MyPlanAgreementAdapter extends BaseAdapter {

	private Context mContext;
	private List<FinancingArgeementEntityItem> mList;
	private LayoutInflater inflater;

	public MyPlanAgreementAdapter(Context context,
			List<FinancingArgeementEntityItem> list) {
		this.mContext = context;
		this.mList = list;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_myagreement_listview,
					parent, false);
			holder.top = convertView.findViewById(R.id.top);
			holder.content = convertView.findViewById(R.id.content);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.tv_number = (TextView) convertView
					.findViewById(R.id.tv_number);
			holder.bottom = convertView.findViewById(R.id.bottom);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (position == 0) {
			holder.top.setVisibility(View.VISIBLE);
		} else {
			holder.top.setVisibility(View.GONE);
		}
		if (position == mList.size() - 1) {
			holder.bottom.setVisibility(View.GONE);
		} else {
			holder.bottom.setVisibility(View.VISIBLE);
		}
		holder.tv_title.setText(mList.get(position).getName());
		if (!TextUtils.isEmpty(mList.get(position).getOrderId())) {
			holder.tv_number.setText(mList.get(position).getOrderId());
		} else {
			holder.tv_number.setText("");
		}
		holder.content.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				Intent intent = new Intent(mContext, KkdPdfWebActivity.class);
				intent.putExtra("url", mList.get(position)
						.getContractsDetailUrl());
				intent.putExtra("pdfUrl", mList.get(position).getContractUrl());
				intent.putExtra("fileName", "[KKD]"
						+ mList.get(position).getName()
						+ mList.get(position).getOrderId() + ".pdf");
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		View top;
		View content;
		TextView tv_title;
		TextView tv_number;
		View bottom;
	}
}
